/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
 var spiralOrder = function (matrix) {
    let left = 0
    let right = matrix[0].length - 1
    let top = 0
    let bottom = matrix.length - 1
    let res = []
    while (left <= right && top <= bottom) {
      for (let i = left; i <= right; i++) {
        res.push(matrix[top][i])
      }
      top++
      for (let i = top; i <= bottom; i++) {
        res.push(matrix[i][right])
      }
      right--
      // 注意到，可能在循环途中，突然不再满足循环的条件，即top > bottom或left > right，其中一对边界彼此交错了
      if (left > right || top > bottom) break; 
      for (let i = right; i >= left; i--) {
        res.push(matrix[bottom][i])
      }
      bottom--
      for (let i = bottom; i >= top; i--) {
        res.push(matrix[i][left])
      }
      left++
    }
    return res;
  };